//
//  SceneViewController.m
//  TodatPlay
//
//  Created by dllo on 16/9/2.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "SceneViewController.h"
#import "LocalCollectionViewCell.h"
#import "NationwideCollectionViewCell.h"
#import "TAMacro.h"
#import "UIView+Frame.h"
#import "MJRefresh.h"

#define kLocalCollectionViewCell @"localReuse"
#define kNationwideCollectionViewCell @"nationwideReuse"
@interface SceneViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *sceneCollectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *sceneLayout;
@property (weak, nonatomic) IBOutlet UIView *slider;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftLayout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightLayout;
@property (weak, nonatomic) IBOutlet UILabel *localLabel;
@property (weak, nonatomic) IBOutlet UILabel *nationwideLabel;



@end

@implementation SceneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    [self configCollectionView];
    [self addTouchOfLabel];
    
    
}
#pragma mark - 注册cell和设置layout属性
- (void)configCollectionView {
    [self.sceneCollectionView registerNib:[UINib nibWithNibName:@"LocalCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kLocalCollectionViewCell];
    [self.sceneCollectionView registerNib:[UINib nibWithNibName:@"NationwideCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kNationwideCollectionViewCell];
    self.sceneLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.sceneLayout.minimumLineSpacing = 0;
    self.sceneLayout.minimumInteritemSpacing = 0;
    self.sceneCollectionView.pagingEnabled = YES;
    self.sceneCollectionView.bounces = NO;
    self.sceneLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
}
#pragma mark - 返回cell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 2;
}
#pragma mark - 返回cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
    LocalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kLocalCollectionViewCell forIndexPath:indexPath];
    
        return cell;
    } else {
        NationwideCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kNationwideCollectionViewCell forIndexPath:indexPath];
        return cell;
    }
           
}
#pragma mark - 返回每个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 104);
}
#pragma mark - 根据偏移量设置滑动条和字体变化
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView.contentOffset.x / SCREEN_WIDTH > 0.98) {
       
        [UIView animateWithDuration:0.4 animations:^{
             self.slider.dm_centerX = SCREEN_WIDTH / 4.0 * 3;
            self.nationwideLabel.textColor = [UIColor blackColor];
            self.localLabel.textColor = [UIColor colorWithWhite:0.702 alpha:1.000];
        }];
    } else if (scrollView.contentOffset.x / SCREEN_WIDTH < 0.05) {
        [UIView animateWithDuration:0.4 animations:^{
            self.slider.dm_centerX = SCREEN_WIDTH / 4.0;
            self.localLabel.textColor = [UIColor blackColor];
            self.nationwideLabel.textColor = [UIColor colorWithWhite:0.600 alpha:1.000];
        }];
    }
    
}



#pragma mark - 给label加手势
- (void)addTouchOfLabel {
    UITapGestureRecognizer *localTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickLocal:)];
    localTap.numberOfTapsRequired = 1;
    UITapGestureRecognizer *nationwideTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickNationwide:)];
    nationwideTap.numberOfTapsRequired = 1;
    [self.localLabel addGestureRecognizer:localTap];
    [self.nationwideLabel addGestureRecognizer:nationwideTap];
    // 开启用户交互
    self.localLabel.userInteractionEnabled = YES;
    self.nationwideLabel.userInteractionEnabled = YES;
}
// 实现手势方法
- (void)clickLocal:(UITapGestureRecognizer *)tap {
    
    self.leftLayout.priority = 751;
    self.rightLayout.priority = 750;
    [UIView animateWithDuration:0.4 animations:^{
        [self.view layoutIfNeeded];
        self.sceneCollectionView.contentOffset = CGPointMake(0.5, 0);
        
    }];
    self.localLabel.textColor = [UIColor blackColor];
    self.nationwideLabel.textColor = [UIColor colorWithWhite:0.600 alpha:1.000];
}
- (void)clickNationwide:(UITapGestureRecognizer *)tap {
    
    self.leftLayout.priority = 750;
    self.rightLayout.priority = 751;
    [UIView animateWithDuration:0.4 animations:^{
        [self.view layoutIfNeeded];
        self.sceneCollectionView.contentOffset = CGPointMake(SCREEN_WIDTH - 0.5, 0);
    }];
    self.nationwideLabel.textColor = [UIColor blackColor];
    self.localLabel.textColor = [UIColor colorWithWhite:0.702 alpha:1.000];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
